DragUtil.userDragArray=[];
DragUtil.ghostUserElement = null ;
DragUtil.getGhostUserElement = function(){
	if(!DragUtil.ghostUserElement){
		DragUtil.ghostUserElement = document.createElement("SPAN");
		DragUtil.ghostUserElement.className = "span_move";
		DragUtil.ghostUserElement.style.border = "2px dashed #aaa";
		DragUtil.ghostUserElement.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	return DragUtil.ghostUserElement;
};
// 计算每个可拖拽的元素的坐标
DragUtil.reCalculateUser = function(el) {
	for( var i = 0,ilen=DragUtil.userDragArray.length; i < ilen; i++ ) {
		var ele = DragUtil.userDragArray[i];
		var position = Position.positionedOffset(ele.elm);
		ele.elm.pagePosLeft = position[0];
		ele.elm.pagePosTop = position[1];
	}

};
var SelectUserDragDrop = Class.create();
SelectUserDragDrop.prototype = {
	initialize: function(elementHeader_id , element_id,canvas_id){
		var element = typeof(element_id)=="string"?document.getElementById(element_id):element_id;
		var elementHeader = typeof(elementHeader_id)=="string"?document.getElementById(elementHeader_id):elementHeader_id;
		var canvas = canvas_id?(typeof(canvas_id) == "string"?document.getElementById(canvas_id):canvas_id):null;
		this._dragStart = ((typeof this.start_Drag == "function") ? this.start_Drag : user_start_Drag);
		this._drag = ((typeof this.when_Drag == "function") ? this.when_Drag : user_when_Drag);
		this._dragEnd = ((typeof this.end_Drag == "function") ? this.end_Drag : user_end_Drag);
		this._afterDrag = ((typeof this.after_Drag == "function") ? this.after_Drag : user_after_Drag);
		this.isDragging = false;
		this.elm = element;
		this.header = elementHeader;
		this.canvas = canvas;
		this.hasIFrame = this.elm.getElementsByTagName("IFRAME").length > 0;
		if( this.header) {
			this.header.style.cursor = "move";
			Milan.drag.init(this.header, this.elm, this.canvas,false);
			this.elm.onDragStart = this._dragStart.bind(this);
			this.elm.onDrag = this._drag.bind(this);
			this.elm.onDragEnd = this._dragEnd.bind(this);
		}
	}
	
};
// 开始拖拽
function user_start_Drag(){
	DragUtil.reCalculateUser(this);	// 重新计算所有可拖拽元素的位置
	this.origNextSibling = this.elm.nextSibling;
	var _ghostElement = DragUtil.getGhostUserElement();
	var offH = this.elm.offsetHeight;
	var offW = this.elm.offsetWidth;
	if(DragUtil.isGecko){	// 修正 Gecko 引擎的怪癖
		offH -= parseInt(_ghostElement.style.borderTopWidth)  *   2 ;
		offW -= parseInt(window.getComputedStyle ? window.getComputedStyle(this.elm, null)["paddingLeft"] : (this.elm.currentStyle ? this.elm.currentStyle["paddingLeft"] : 0));
		
	}	

	//this.elm.className = "";
	var position = Position.positionedOffset(this.elm);
	var offLeft = position[0];
	var offTop = position[1];

	// 在元素的前面插入占位虚线框
	jQuery(_ghostElement).css({width:"90px",height: offH + "px"});
	this.elm.parentNode.insertBefore(_ghostElement,  this.elm.nextSibling);
	
	// 设置元素样式属性
	this.elm.style.width = offW + "px";
	this.elm.style.position = "absolute";
	this.elm.style.zIndex = 100;
	this.elm.style.left = offLeft + "px";
	this.elm.style.top = offTop + "px";
	this.isDragging = false;
	return false;
}
// 拖动时触发这个函数（每次鼠标坐标变化时）
function user_when_Drag(clientX , clientY){
	if (!this.isDragging){	// 第一次移动鼠标，设置它的样式
		this.elm.style.filter = "alpha(opacity=70)";
		this.elm.style.opacity = 0.7;
		this.isDragging = true;
	}
	// 计算离当前鼠标位置最近的可拖拽的元素，把该元素放到 found 变量中
	var found = null;
	var max_distance = 100000000;
	var that = this;
	for(var i = 0,ilen=DragUtil.userDragArray.length; i <ilen; i++) {
		var ele = DragUtil.userDragArray[i];
		var distance = Math.sqrt(Math.pow(clientX - ele.elm.pagePosLeft, 2 ) + Math.pow(clientY - ele.elm.pagePosTop, 2 ));
		if(ele == this ) {
			continue;
		}
		if(isNaN(distance)){
			continue;
		}
		if(distance < max_distance){
			max_distance = distance;
			found = ele;
		}
	}
	// 把虚线框插到 found 元素的前面
	var _ghostElement = DragUtil.getGhostUserElement();
	if(found != null && _ghostElement.nextSibling != found.elm) {
		found.elm.parentNode.insertBefore(_ghostElement, found.elm);
		if(DragUtil.isOpera){	// Opera 的毛病
			document.body.style.display = "none";
			document.body.style.display = "";
		}
	}
}

// 结束拖拽
function user_end_Drag(){
	if(this._afterDrag()){
		jQuery("#dragcontainer1").children().removeClass().addClass("span_move");
		jQuery("#dragcontainer1").children(":last").removeClass().addClass("span_move_end");
	}
	//this.elm.className = PORTLET_CLASS;
	return true;
}

// 结束拖拽时调用的函数
function user_after_Drag(){
	var returnValue = false;
	
	// 把拖动的元素的样式回复到原来的状态
	this.elm.style.position = "";
	this.elm.style.top = "";
	this.elm.style.left = "";
	this.elm.style.width = "";
	this.elm.style.zIndex = "";
	this.elm.style.filter = "";
	this.elm.style.opacity = "";
	
	// 在虚线框的地方插入拖动的这个元素
	var ele = DragUtil.getGhostUserElement();
	if(ele.nextSibling != this.origNextSibling) {
		ele.parentNode.insertBefore(this.elm, ele.nextSibling);
		returnValue = true;
	}
	// 删除虚线框
	ele.parentNode.removeChild(ele);
	if(DragUtil.isOpera) {
		document.body.style.display = "none";
		document.body.style.display = "" ;
	}
	return returnValue;
}